package day1.linkedlist;

/**
 * @author pacai
 * @version 1.0
 */
@SuppressWarnings("all")
public class DoubleReverseLinkedList {
    Node head;
    Node tail;


    public Node reverse(Node head) {
        Node next = null;
        Node pre = null;
        while(head != null){
            next = head.next;
            head.next = pre;
            head.last = next;
            pre = head;
            head = next;
        }
        return pre;
    }


    public void printLinkedList(Node head) {
        while (head != null) {
            System.out.println(head.data);
            head = head.next;
        }
    }


    private class Node<V> {
        V data;
        Node<V> next;
        Node<V> last;

        public Node(V data) {
            this.data = data;
        }
    }



    public class DoubleQueue<V>{
        private Node<V> head;
        private Node<V> tail;
        private int size;

        public DoubleQueue() {
            this.head = null;
            this.tail = null;
            this.size = 0;
        }

        public int size(){
            return size;
        }

        public boolean isEmpty(){
            return size == 0;
        }

        public void pushHead(V data){
            Node<V> newNode = new Node<>(data);
            if(head == null){
                head = newNode;
                tail = newNode;
            }else{
                newNode.next = head;
                head.last = newNode;
                head = newNode;
            }
        }


        public void pushTail(V data){
            Node<V> newNode = new Node<>(data);
            if(head == null){
                head = newNode;
                tail = newNode;
            }else{
                newNode.last =  tail;
                tail.next = newNode;
                tail = newNode;
            }
        }

        public V popHead(){
            V ans = null;
            if(head == null){
                return ans;
            }
            ans = head.data;
            if(head == tail){
                head = tail = null;
            }else{
                head = head.next;
                head.last = null;
            }
            return ans;
        }

        public V popTail(){
            V ans = null;
            if(head == null){
                return ans;
            }
            ans = tail.data;
            if(tail == head){
                head = tail = null;
            }else{
                tail = tail.last;
                tail.next = null;
            }
            return ans;
        }

        public V peekHead(){
            return head.data == null ? null : head.data;
        }

        public V peekTail(){
            return tail.data == null ? null : tail.data;
        }

        public void delete(){
            while(true){
                if(head == null){
                    break;
                }
                head = head.next;
                if(head == tail){
                    head = tail = null;
                }
            }
        }


        @Override
        protected void finalize() throws Throwable {
            delete();
        }
    }

}
